package com.andy.base.utils

import android.os.Parcelable
import com.andy.base.AppHelper
import com.tencent.mmkv.MMKV

/**
 * Author:Andy
 * Date:2023/3/21 11:32
 * Description:MVUtils
 */
object MVUtils {
    /**
     * 初始化MMKV相关SDK
     */
    fun initMMKV() {
        val rootDir = AppHelper.mContext.filesDir.absolutePath + "/avatar_mmkv"
        MMKV.initialize(AppHelper.mContext, rootDir)
    }

    /**
     * 存储常规数据
     */
    fun put(key: String, any: Any?): Boolean {
        if (key.isEmpty() || any == null) {
            return false
        }
        val avatarMMKV = MMKV.defaultMMKV()
        try {
            when (any) {
                is String -> {
                    avatarMMKV.encode(key, any)
                }
                is Double -> {
                    avatarMMKV.encode(key, any)
                }
                is Float -> {
                    avatarMMKV.encode(key, any)
                }
                is Int -> {
                    avatarMMKV.encode(key, any)
                }
                is Parcelable -> {
                    avatarMMKV.encode(key, any)
                }
                is ByteArray -> {
                    avatarMMKV.encode(key, any)
                }
                is Long -> {
                    avatarMMKV.encode(key, any)
                }
                is Boolean -> {
                    avatarMMKV.encode(key, any)
                }
                else -> {
                    return false
                }
            }
        } catch (exception: Exception) {
            return false
        }
        return true
    }

    /**
     * 存储set集合
     */
    fun putSet(key: String, content: Set<String>?): Boolean {
        if (key.isEmpty() || content == null) {
            return false
        }
        val avatarMMKV = MMKV.defaultMMKV()
        avatarMMKV.encode(key, content)
        return true
    }

    /**
     * 获取Boolean数据
     */
    fun getBoolean(key: String): Boolean {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeBool(key)
    }

    /**
     * 获取Boolean数据
     */
    fun getBoolean(key: String, default: Boolean): Boolean {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeBool(key, default)
    }

    /**
     * 获取String 数据
     */
    fun getString(key: String): String {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeString(key) ?: ""
    }

    /**
     * 获取String 数据
     */
    fun getString(key: String, default: String): String {
        val avatarMMKV = MMKV.defaultMMKV()
        val value = avatarMMKV.decodeString(key)
        return if (value.isNullOrEmpty()) {
            default
        } else {
            value
        }
    }

    /**
     * 获取Int数据
     */
    fun getInt(key: String): Int {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeInt(key)
    }

    /**
     * 获取Int数据
     */
    fun getInt(key: String, default: Int): Int {
        val avatarMMKV = MMKV.defaultMMKV()
        val value = avatarMMKV.decodeInt(key)
        return if (value == 0) {
            default
        } else {
            avatarMMKV.decodeInt(key)
        }
    }

    /**
     * 获取Long数据
     */
    fun getLong(key: String): Long {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeLong(key)
    }

    /**
     * 获取Long数据
     */
    fun getLong(key: String, default: Long): Long {
        val avatarMMKV = MMKV.defaultMMKV()
        val value = avatarMMKV.decodeLong(key)
        return if (value == 0L) {
            default
        } else {
            avatarMMKV.decodeLong(key)
        }
    }

    /**
     * 获取Double数据
     */
    fun getDouble(key: String): Double {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeDouble(key)
    }

    /**
     * 获取Double数据
     */
    fun getDouble(key: String, default: Double): Double {
        val avatarMMKV = MMKV.defaultMMKV()
        val value = avatarMMKV.decodeDouble(key)
        return if (value == 0.0) {
            default
        } else {
            value
        }
    }

    /**
     * 获取Float数据
     */
    fun getFloat(key: String): Float {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeFloat(key)
    }

    /**
     * 获取Float数据
     */
    fun getFloat(key: String, default: Float): Float {
        val avatarMMKV = MMKV.defaultMMKV()
        val value = avatarMMKV.decodeFloat(key)
        return if (value == 0.0F) {
            default
        } else {
            avatarMMKV.decodeFloat(key)
        }
    }

    /**
     * 获取字节数组
     */
    fun getByteArray(key: String): ByteArray? {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeBytes(key)
    }

    /**
     * 获取字节数组
     */
    fun getByteArray(key: String, default: Int): ByteArray? {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeBytes(key)
    }

    /**
     * 获取序列化对象
     */
    fun <T : Parcelable> getParcelable(key: String, clazz: Class<T>): T? {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeParcelable(key, clazz)
    }

    /**
     * 获取Set集合
     */
    fun getSet(key: String): Set<String>? {
        val avatarMMKV = MMKV.defaultMMKV()
        return avatarMMKV.decodeStringSet(key)
    }

    //   删除数据
    fun remove(key: String) {
        val avatarMMKV = MMKV.defaultMMKV()
        avatarMMKV.removeValueForKey(key)
    }

    // 删除多个数据
    fun remove(vararg key: String) {
        val avatarMMKV = MMKV.defaultMMKV()
        val keys = arrayOfNulls<String>(key.size)
        key.forEachIndexed { index, content ->
            keys[index] = content
        }
        avatarMMKV.removeValuesForKeys(keys)
    }

    // 清理所有数据
    fun clearAll() {
        val avatarMMKV = MMKV.defaultMMKV()
        avatarMMKV.clearAll()
    }

}